package com.example.music;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import com.example.lwx.R;

public class MusicService extends Service {
    MediaPlayer mePlayer;
    public MusicService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mePlayer=MediaPlayer.create(this, R.raw.longquanfeng);
        mePlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mePlayer.isLooping();
    }
    class MyBinder extends Binder{
        public void play(){//用户字定义方法
            if(mePlayer!=null&&!mePlayer.isPlaying()){
                mePlayer.start();
            }
        }
        public void stop(){
            if(mePlayer!=null&&mePlayer.isPlaying()){
                mePlayer.stop();
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        // throw new UnsupportedOperationException("Not yet implemented");
        return  new MyBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        mePlayer.stop();
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mePlayer!=null){
            mePlayer.release();
            mePlayer=null;
        }
    }
}